package com.alienlab.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alienlab.utils.NewsJsoup;

import com.alienlab.utils.NewsLoadUrlRun;
import com.alienlab.utils.NowTime;
import com.alienlab.utils.PropertyConfig;
import org.omg.CORBA.Request;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Properties;

@Controller
@RequestMapping(value = "/news")
public class NewsController {
	@RequestMapping(value = "/index")
	public String getindex(){
		return "new_file";
	}
	@RequestMapping(value = "/demo")
	public String getdemo(){
		return "demo05";
	}
	private Properties propertyConfig = new PropertyConfig("jdbc.properties").getProperties();
	private String news_url=  propertyConfig.getProperty("url");
	private String dummy_path=  propertyConfig.getProperty("dummy-path");



	@RequestMapping(value = "/testInterface")
	@ResponseBody
	public String testInterface(HttpServletRequest request ,HttpServletResponse response){
		String param  = request.getParameter("param");
		JSONArray result = new JSONArray();
        for (int i=0;i<4;i++){
			JSONObject jo = new JSONObject();
			jo.put("t0",param+"请求第"+String.valueOf(i)+"行");
			result.add(jo);
		}
		return result.toJSONString();
	}

    /**
	 * 获取到所有的pdf下载的地址，并且返回前台
	 */
	@RequestMapping(value = "/getUrl")
    public String getUrl(HttpServletRequest request ,HttpServletResponse response){
    	String geturl = request.getParameter("newarrurl");
    	String today = NowTime.getNowTime("yyyy-MM-dd");
		JSONArray jsonArray = new JSONArray();
		NewsJsoup newsJsoup = new NewsJsoup();
		long end=System.currentTimeMillis();
		try {
			jsonArray = newsJsoup.getNeedFile_(geturl, "", dummy_path);
			System.out.println(jsonArray);
		} catch (IOException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		JSONArray jsonArray2 = new JSONArray();
		for(int i = 0; i<jsonArray.size();i++){
			jsonArray2.add(""+news_url+""+today+"/"+today+"-"+end+"-"+i+".pdf");
		}
		NewsLoadUrlRun newsLoadUrlRun = new NewsLoadUrlRun( jsonArray, end);
		Thread rThread  = new Thread(newsLoadUrlRun);
		rThread.start();
/*		NewsManage.initAnyscDownloader();//只能调用一次
		NewsManage newsLoadStart = new NewsManage();
	try {
		jsonArray = newsLoadStart.getLoadPdf("http://xh.xhby.net/mp2/html/2016-06/14/node_2.htm");
	} catch (Exception e) {
		System.out.println("错误");
	}*/
	try {
		response.getWriter().write(jsonArray2.toString());
	} catch (IOException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
		return "";
    }
    public static void main(String[] args) {
		/*NewsController newsController = new NewsController();
		newsController.getUrl();*/
	}
}
